package service

import (
	"strconv"
	"sugar/controller"
	"sugar/errorcode"
	"sugar/utils"

	"github.com/gin-gonic/gin"
	"gopkg.in/guregu/null.v3"
)

type createModelParams struct {
	Name           null.String `form:"name"`
	ModelClass     null.String `form:"model_class"`
	ModelFileBytes []byte      `form:"model_file_bytes"`
	Dependencies   null.String `form:"dependencies"`
	Description    null.String `form:"description"`
	ModelType      null.Int    `form:"model_type"`
	ModelFileType  null.Int    `form:"model_file_type"`
	ModelFileName  null.String `form:"model_file_name"`
	Public         int         `form:"public"`
}

type getModelParams struct {
	ID null.Int `form:"id"`
}

type listModelParams struct {
	PageSize int `json:"page_size"`
	PageNo   int `json:"page_no"`
}

// @Summary 创建模型
// @Description 创建模型
// @Tags 模型管理
// @Accept  json
// @Produce json
// @Param   params     body    createDatasetParams     true        "创建模型参数"
// @Success 200 {object}  response.Response	"Success data为null"
// @Failure 500  {object}  response.Response  "内部错误 data为null"
// @Failure 401  {object}  response.Response  "token错误 data为null"
// @Failure 404  {object}  response.Response  "无法访问 data为null"
// @Router /model [post]
func createModel(c *gin.Context, tokenMap map[string]interface{}) {
	paramOk := utils.CheckUUID4MustExist(tokenMap["user_id"])
	if !paramOk {
		panic(errorcode.ParamError)
	}
	name, bexist := c.GetPostForm("name")
	if !bexist {
		panic(errorcode.ParamError)
	}
	modelClass, bexist := c.GetPostForm("model_class")
	if !bexist {
		panic(errorcode.ParamError)
	}
	dependencies := null.NewString("", false)
	if dep, bexist := c.GetPostForm("dependencies"); bexist {
		dependencies.SetValid(dep)
	}
	description := null.NewString("", false)
	if des, bexist := c.GetPostForm("description"); bexist {
		description.SetValid(des)
	}
	modelType, err := strconv.Atoi(c.DefaultPostForm("model_type", "0"))
	if err != nil {
		panic(errorcode.ParamError)
	}
	modelFileType, err := strconv.Atoi(c.DefaultPostForm("model_file_type", "0"))
	if err != nil {
		panic(errorcode.ParamError)
	}
	public, err := strconv.Atoi(c.DefaultPostForm("public", "0"))
	if err != nil {
		panic(errorcode.ParamError)
	}
	modelFileName := null.NewString("", false)
	if mfn, bexist := c.GetPostForm("model_file_name"); bexist {
		modelFileName.SetValid(mfn)
	}
	fileHeader, err := c.FormFile("model_file_bytes")
	if err != nil {
		panic(errorcode.ParamError)
	}
	file, err := fileHeader.Open()
	defer file.Close()
	modelFileBytes := make([]byte, fileHeader.Size)
	size, err := file.Read(modelFileBytes)
	if int64(size) != fileHeader.Size || err != nil {
		panic(errorcode.ParamError)
	}
	// jsonParams := createModelParams{
	// 	Public: 0,
	// }
	// err := c.ShouldBind(&jsonParams)
	// if err != nil {
	// 	panic(errorcode.ParamError)
	// }
	// paramOk = utils.CheckStringMustExist(jsonParams.Name, jsonParams.ModelClass) && utils.CheckIntMustExist(jsonParams.ModelType, jsonParams.ModelFileType) && utils.CheckStringIfExist(jsonParams.Description, jsonParams.Dependencies)
	// if !paramOk {
	// 	panic(errorcode.ParamError)
	// }
	// if jsonParams.ModelFileType.Int64 == int64(constant.ZipModel) {
	// 	paramOk = utils.CheckStringMustExist(jsonParams.ModelFileName)
	// 	if !paramOk {
	// 		panic(errorcode.ParamError)
	// 	}
	// }
	modelOp := controller.NewOp()
	res := modelOp.CreateModel(tokenMap["user_id"].(string), name, modelClass, modelFileBytes, dependencies, description, modelType, modelFileType, modelFileName, public)
	c.JSON(res.StatusCode, res)
}

// @Summary 模型详情
// @Description 模型详情
// @Tags 模型管理
// @Accept  json
// @Produce json
// @Param   params     body    getDatasetParams     true        "模型ID"
// @Success 200 {object}  response.Response	"Success data为null"
// @Failure 500  {object}  response.Response  "内部错误 data为null"
// @Failure 401  {object}  response.Response  "token错误 data为null"
// @Failure 404  {object}  response.Response  "无法访问 data为null"
// @Router /model [get]
func getModel(c *gin.Context, tokenMap map[string]interface{}) {
	paramOk := utils.CheckUUID4MustExist(tokenMap["user_id"])
	if !paramOk {
		panic(errorcode.ParamError)
	}
	var jsonParams getModelParams
	err := c.BindQuery(&jsonParams)
	if err != nil {
		panic(errorcode.ParamError)
	}
	paramOk = utils.CheckIntMustExist(jsonParams.ID)
	if !paramOk {
		panic(errorcode.ParamError)
	}
	modelOp := controller.NewOp()
	res := modelOp.GetModel(tokenMap["user_id"].(string), jsonParams.ID.Int64)
	c.JSON(res.StatusCode, res)
}

// @Summary 模型列表
// @Description 模型列表
// @Tags 模型管理
// @Accept  json
// @Produce json
// @Success 200 {object}  response.Response	"Success data为null"
// @Failure 500  {object}  response.Response  "内部错误 data为null"
// @Failure 401  {object}  response.Response  "token错误 data为null"
// @Failure 404  {object}  response.Response  "无法访问 data为null"
// @Router /model/list [get]
func listModel(c *gin.Context, tokenMap map[string]interface{}) {
	paramOk := utils.CheckUUID4MustExist(tokenMap["user_id"])
	if !paramOk {
		panic(errorcode.ParamError)
	}
	jsonParams := listModelParams{
		PageNo:   0,
		PageSize: 10,
	}
	err := c.BindQuery(&jsonParams)
	if err != nil {
		panic(errorcode.ParamError)
	}
	paramOk = utils.CheckIntMustExist(jsonParams.PageNo, jsonParams.PageSize)
	modelOp := controller.NewOp()
	res := modelOp.ListModel(tokenMap["user_id"].(string), jsonParams.PageSize, jsonParams.PageNo)
	c.JSON(res.StatusCode, res)
}
